package com.css.fxfzdpzs.modules.ypg.service;

import com.css.fxfzdpzs.modules.dzzh.entity.first.DzzhRiskAreaAnalysis;
import com.css.fxfzdpzs.modules.ypg.entity.YpgMapGeom;
import com.css.fxfzdpzs.modules.ypg.entity.first.*;
import com.css.fxfzdpzs.modules.ypg.entity.second.*;
import com.css.fxfzdpzs.modules.ypg.repository.YpgRepository;
import com.css.fxfzdpzs.modules.ypg.vo.*;
import com.css.fxfzdpzs.util.PlatformObjectUtils;
import com.css.fxfzdpzs.util.UUIDGenerator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author y1fan
 * @description
 */
@Service
public class YpgServiceImpl implements YpgService {


    @Resource
    private YpgRepository ypgRepository;

    @Override
    public YpgEarthquakeSetNum getYpgEarthquakeSetNum() {
        return ypgRepository.getYpgEarthquakeSetNum();
    }

    @Override
    public List<YpgProvinceEarthquakeSet> getYpgProvinceEarthquakeSet() {
        return ypgRepository.getYpgProvinceEarthquakeSets();
    }

    @Override
    public Ypg2015yearsEarthquakeSituation getYpg2015yearsEarthquakeSituation() {
        return ypgRepository.getYpg2015yearsEarthquakeSituation();
    }

    @Override
    public List<YpgEarthquakeSetTrend> getYpgEarthquakeSetTrend() {
        return ypgRepository.getYpgEarthquakeSetTrends();
    }

    @Override
    public YpgSceneCheckSituationVO getYpgSceneCheckSituation(int curPage, int pageSize) {
        YpgSceneCheckSituationVO vo=new YpgSceneCheckSituationVO();
        YpgSceneCheckSituation entity = ypgRepository.getYpgSceneCheckSituation();
        Map<String,Object> map = ypgRepository.getYpgPersonnel(curPage,pageSize);
        vo.setTrip(entity.getTrip());
        vo.setSurveyPointNum(entity.getSurveyPointNum());
      //  vo.setNumberParticipant((Integer) map.get("total"));
        vo.setNumberParticipant(981);
        vo.setMap(map);

        return vo;
    }

    @Override
    public List<YpgProvinceCoverage> getYpgProvinceCoverageVO(String code) {
        List<YpgProvinceCoverage> ypgProvinceCoverages = ypgRepository.getYpgProvinceCoverages(code);
        return ypgProvinceCoverages;
    }

    @Override
    public BigDecimal getcountyCoverage(String code) {
        YpgCountyCoverage ypgCountyCoverage= ypgRepository.getCountCoverage(code);
        if (PlatformObjectUtils.isEmpty(ypgCountyCoverage)){
            return BigDecimal.valueOf(0);
        }
        return BigDecimal.valueOf(Double.valueOf(ypgCountyCoverage.getCountyCoverage()));
    }

    @Override
    public List<YpgFirstResultVO> getMap(YpgFirstMapVO ypgFirstMapVO) {
        List<YpgFirstResultVO> list= ypgRepository.getMap(ypgFirstMapVO);

        for (YpgFirstResultVO entity : list) {
            if ("1".equals(ypgFirstMapVO.getType())){
                if (PlatformObjectUtils.isEmpty(entity.getResult())){
                    entity.setResult("0");
                }
                String[] split = entity.getResult().split("-");
                String result="";
                if (split.length==1){
                    result=split[0];
                }else {
                    if (Double.valueOf(split[0])>Double.valueOf(split[1])){
                        result=split[0];
                    }else {
                        result=split[1];
                    }
                }
                entity.setResult(result);
            }
            if ("2".equals(ypgFirstMapVO.getType())){
                if (PlatformObjectUtils.isEmpty(entity.getResult())){
                    entity.setResult("0");
                }
                String[] split = entity.getResult().split("-");
                String result="";
                if (split.length==1){
                    result=split[0];
                }else {
                    if (Double.valueOf(split[0])<Double.valueOf(split[1])){
                        result=split[0];
                    }else {
                        result=split[1];
                    }
                }
                entity.setResult(result);
            }
            if ("3".equals(ypgFirstMapVO.getType())){
                if (PlatformObjectUtils.isEmpty(entity.getResult())){
                    entity.setResult("0");
                }
                String[] split = entity.getResult().split("-");
                String result="";
                if (split.length==1){
                    result=split[0];
                }else {
                    if ((Double.valueOf(split[0])+Double.valueOf(split[1]))%2==0){
                        result=String.valueOf(((Double.valueOf(split[0])+Double.valueOf(split[1]))/2)+1);
                    }else {
                        result=String.valueOf((Double.valueOf(split[0])+Double.valueOf(split[1]))/2);
                    }
                }
                entity.setResult(result);
            }
        }
        if (PlatformObjectUtils.isNotEmpty(ypgFirstMapVO.getNumber())){
            List<YpgFirstResultVO> collect = list.stream().filter(n -> Double.valueOf(n.getResult()) > ypgFirstMapVO.getNumber()).collect(Collectors.toList());
           ArrayList<YpgFirstResultVO> collect1 = collect.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getLatitude() + ";" + o.getLongitude()))), ArrayList::new));
            return collect1;
        }else {
          //  ArrayList<YpgFirstResultVO> collect1 = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getLatitude() + ";" + o.getLongitude()))), ArrayList::new));
            return list;
        }
    }

    @Override
    public void getGemo() {
        //此方法未做原数据删除操作；
        List<YpgFirstMap> list= ypgRepository.getFirstMap();
        List<YpgMapGeom> mapGemos=new ArrayList<>();
        for (YpgFirstMap ypgFirstMap : list) {
            if (PlatformObjectUtils.isEmpty(ypgFirstMap.getDie())){
                ypgFirstMap.setDie("0");
            }
            if (PlatformObjectUtils.isEmpty(ypgFirstMap.getInjury())){
                ypgFirstMap.setInjury("0");
            }
            if (PlatformObjectUtils.isEmpty(ypgFirstMap.getTent())){
                ypgFirstMap.setTent("0");
            }
            if (PlatformObjectUtils.isEmpty(ypgFirstMap.getRescue())){
                ypgFirstMap.setRescue("0");
            }
            if (PlatformObjectUtils.isEmpty(ypgFirstMap.getPersonnel())){
                ypgFirstMap.setPersonnel("0");
            }
            YpgMapGeom mapGemo=new YpgMapGeom();
            mapGemo.setId(UUIDGenerator.getUUID());
            mapGemo.setLongitude(ypgFirstMap.getLongitude());
            mapGemo.setLatitude(ypgFirstMap.getLatitude());
            mapGemo.setGeom(ypgFirstMap.getGeom());
            mapGemo.setMm(ypgFirstMap.getMm());

            String die = ypgFirstMap.getDie();
            String injury = ypgFirstMap.getInjury();
            String tent = ypgFirstMap.getTent();
            String personnel = ypgFirstMap.getPersonnel();
            String rescue = ypgFirstMap.getRescue();

            String[] dieSplit = die.split("-");
            String[] injurySplit = injury.split("-");
            String[] tentSplit = tent.split("-");
            String[] personnelSplit = personnel.split("-");
            String[] rescueSplit = rescue.split("-");
            if (dieSplit.length==1){
                mapGemo.setDieAvg(dieSplit[0]);
                mapGemo.setDieMax(dieSplit[0]);
                mapGemo.setDieMin(dieSplit[0]);
            }else {
                if (Double.valueOf(dieSplit[0])>Double.valueOf(dieSplit[1])){
                    mapGemo.setDieMax(dieSplit[0]);
                    mapGemo.setDieMin(dieSplit[1]);
                    if ((Double.valueOf(dieSplit[0])+Double.valueOf(dieSplit[1]))%2==0){
                       mapGemo.setDieAvg(String.valueOf(((Integer.valueOf(dieSplit[0])+Integer.valueOf(dieSplit[1]))/2)));
                    }else {
                        mapGemo.setDieAvg(String.valueOf(((Integer.valueOf(dieSplit[0])+Integer.valueOf(dieSplit[1]))/2)+1));
                    }
                }else {
                    mapGemo.setDieMax(dieSplit[1]);
                    mapGemo.setDieMin(dieSplit[0]);
                    if ((Double.valueOf(dieSplit[0])+Double.valueOf(dieSplit[1]))%2==0){
                        mapGemo.setDieAvg(String.valueOf(((Integer.valueOf(dieSplit[0])+Integer.valueOf(dieSplit[1]))/2)));
                    }else {
                        mapGemo.setDieAvg(String.valueOf(((Integer.valueOf(dieSplit[0])+Integer.valueOf(dieSplit[1]))/2)+1));
                    }
                }
            }

            if (injurySplit.length==1){
                mapGemo.setInjuryMax(injurySplit[0]);
                mapGemo.setInjuryMin(injurySplit[0]);
                mapGemo.setInjuryAvg(injurySplit[0]);
            }else {
                if (Double.valueOf(injurySplit[0])>Double.valueOf(injurySplit[1])){
                    mapGemo.setInjuryMax(injurySplit[0]);
                    mapGemo.setInjuryMin(injurySplit[1]);
                    if ((Double.valueOf(injurySplit[0])+Double.valueOf(injurySplit[1]))%2==0){
                        mapGemo.setInjuryAvg(String.valueOf(((Integer.valueOf(injurySplit[0])+Integer.valueOf(injurySplit[1]))/2)));
                    }else {
                        mapGemo.setInjuryAvg(String.valueOf(((Integer.valueOf(injurySplit[0])+Integer.valueOf(injurySplit[1]))/2)+1));
                    }
                }else {
                    mapGemo.setInjuryMax(injurySplit[1]);
                    mapGemo.setInjuryMin(injurySplit[0]);
                    if ((Double.valueOf(injurySplit[0])+Double.valueOf(injurySplit[1]))%2==0){
                        mapGemo.setInjuryAvg(String.valueOf(((Integer.valueOf(injurySplit[0])+Integer.valueOf(injurySplit[1]))/2)));
                    }else {
                        mapGemo.setInjuryAvg(String.valueOf(((Integer.valueOf(injurySplit[0])+Integer.valueOf(injurySplit[1]))/2)+1));
                    }
                }
            }


            if (tentSplit.length==1){
                mapGemo.setTentMax(tentSplit[0]);
                mapGemo.setTentMin(tentSplit[0]);
                mapGemo.setTentAvg(tentSplit[0]);
            }else {
                if (Double.valueOf(tentSplit[0])>Double.valueOf(tentSplit[1])){
                    mapGemo.setTentMax(tentSplit[0]);
                    mapGemo.setTentMin(tentSplit[1]);
                    if ((Double.valueOf(tentSplit[0])+Double.valueOf(tentSplit[1]))%2==0){
                        mapGemo.setTentAvg(String.valueOf(((Integer.valueOf(tentSplit[0])+Integer.valueOf(tentSplit[1]))/2)));
                    }else {
                        mapGemo.setTentAvg(String.valueOf(((Integer.valueOf(tentSplit[0])+Integer.valueOf(tentSplit[1]))/2)+1));
                    }
                }else {
                    mapGemo.setTentMax(tentSplit[1]);
                    mapGemo.setTentMin(tentSplit[0]);
                    if ((Double.valueOf(tentSplit[0])+Double.valueOf(tentSplit[1]))%2==0){
                        mapGemo.setTentAvg(String.valueOf(((Integer.valueOf(tentSplit[0])+Integer.valueOf(tentSplit[1]))/2)));
                    }else {
                        mapGemo.setTentAvg(String.valueOf(((Integer.valueOf(tentSplit[0])+Integer.valueOf(tentSplit[1]))/2)+1));
                    }
                }
            }

            if (personnelSplit.length==1){
                mapGemo.setPersonnelMax(personnelSplit[0]);
                mapGemo.setPersonnelMin(personnelSplit[0]);
                mapGemo.setPersonnelAvg(personnelSplit[0]);
            }else {
                if (Double.valueOf(personnelSplit[0])>Double.valueOf(personnelSplit[1])){
                    mapGemo.setPersonnelMax(personnelSplit[0]);
                    mapGemo.setPersonnelMin(personnelSplit[1]);
                    if ((Double.valueOf(personnelSplit[0])+Double.valueOf(personnelSplit[1]))%2==0){
                        mapGemo.setPersonnelAvg(String.valueOf(((Integer.valueOf(personnelSplit[0])+Integer.valueOf(personnelSplit[1]))/2)));
                    }else {
                        mapGemo.setPersonnelAvg(String.valueOf(((Integer.valueOf(personnelSplit[0])+Integer.valueOf(personnelSplit[1]))/2)+1));
                    }
                }else {
                    mapGemo.setPersonnelMax(personnelSplit[1]);
                    mapGemo.setPersonnelMin(personnelSplit[0]);
                    if ((Double.valueOf(personnelSplit[0])+Double.valueOf(personnelSplit[1]))%2==0){
                        mapGemo.setPersonnelAvg(String.valueOf(((Integer.valueOf(personnelSplit[0])+Integer.valueOf(personnelSplit[1]))/2)));
                    }else {
                        mapGemo.setPersonnelAvg(String.valueOf(((Integer.valueOf(personnelSplit[0])+Integer.valueOf(personnelSplit[1]))/2)+1));
                    }
                }
            }

            if (rescueSplit.length==1){
                mapGemo.setRescueMax(rescueSplit[0]);
                mapGemo.setRescueMin(rescueSplit[0]);
                mapGemo.setRescueAvg(rescueSplit[0]);
            }else {
                if (Double.valueOf(rescueSplit[0])>Double.valueOf(rescueSplit[1])){
                    mapGemo.setRescueMax(rescueSplit[0]);
                    mapGemo.setRescueMin(rescueSplit[1]);
                    if ((Double.valueOf(rescueSplit[0])+Double.valueOf(rescueSplit[1]))%2==0){
                        mapGemo.setRescueAvg(String.valueOf(((Integer.valueOf(rescueSplit[0])+Integer.valueOf(rescueSplit[1]))/2)));
                    }else {
                        mapGemo.setRescueAvg(String.valueOf(((Integer.valueOf(rescueSplit[0])+Integer.valueOf(rescueSplit[1]))/2)+1));
                    }
                }else {
                    mapGemo.setRescueMax(rescueSplit[1]);
                    mapGemo.setRescueMin(rescueSplit[0]);
                    if ((Double.valueOf(rescueSplit[0])+Double.valueOf(rescueSplit[1]))%2==0){
                        mapGemo.setRescueAvg(String.valueOf(((Integer.valueOf(rescueSplit[0])+Integer.valueOf(rescueSplit[1]))/2)));
                    }else {
                        mapGemo.setRescueAvg(String.valueOf(((Integer.valueOf(rescueSplit[0])+Integer.valueOf(rescueSplit[1]))/2)+1));
                    }
                }
            }

            mapGemos.add(mapGemo);
        }

        ypgRepository.saveMapGemo(mapGemos);


    }

    @Override
    public List<YpgProSurveyPointCount> getSurveyPointCount(String code) {
        List<YpgProSurveyPointCount> list = ypgRepository.getYpgProSurveyPointCounts(code);
        return list;
    }

    @Override
    public BigDecimal getVillageCoverage(String code) {
        YpgCountyCoverage ypgCountyCoverage= ypgRepository.getVillageCoverage(code);
        if (PlatformObjectUtils.isEmpty(ypgCountyCoverage)){
            return BigDecimal.valueOf(0);
        }
            return  BigDecimal.valueOf(Double.valueOf(ypgCountyCoverage.getVillageCoverage()));
    }

    @Override
    public List<YpgProvinceCoverage> getProvinceVillageCov(String code) {
        List<YpgProvinceCoverage> list= ypgRepository.getProvinceVillageCov(code);
        return list;
    }

    @Override
    public YpgSurveyPointLocality getSurveyPointLocality(String code) {
        return ypgRepository.getYpgSurveyPointLocality(code);
    }

    @Override
    public List<YpgSurveyPointSituation> getSurveyPointSituation(String code) {
        return ypgRepository.getYpgSurveyPointSituations(code);
    }

    @Override
    public List<YpgProHouseNumCount> getProHouseNumCount(String code) {
        return ypgRepository.getYpgProHouseNumCounts(code);
    }

    @Override
    public List<YpgHouseTypeCount> getHouseTypeCount(String code) {
        return ypgRepository.getYpgHouseTypeCounts(code);
    }

    @Override
    public List<YpgBuildMonomerCount> getBuildMonomerCount(String code) {
        return ypgRepository.getBuildMonomerCount(code);
    }

    @Override
    public List<YpgProDamagePointCount> getDamagePointCount(String code) {
        return ypgRepository.getYpgProDamagePointCounts(code);
    }

    @Override
    public List<YpgDamagePointTypeCount> getDamagePointTypeCount(String code) {
        return ypgRepository.getYpgDamagePointTypeCounts(code);
    }

    @Override
    public List<YpgDamagePointSituationCount> getDamagePointSituationCount(String code) {
        return ypgRepository.getDamagePointSituationCount(code);
    }

    @Override
    public List<YpgProSurveyPointCountMapVO> getSurveyPointCountMap(String code,String flag) {
        List<YpgProSurveyPointCountMapVO> list=ypgRepository.getSurveyPointCountMap(code,flag);
        return list;
    }



}
